//房屋列表页
import 'package:enjoy_plus_hm/pages/house/components/house_item.dart';
import 'package:enjoy_plus_hm/utils/eventBus.dart';
import 'package:enjoy_plus_hm/utils/request.dart';
import 'package:enjoy_plus_hm/utils/toast.dart';
import 'package:flutter/material.dart';

class HouseList extends StatefulWidget {
  const HouseList({super.key});

  @override
  State<HouseList> createState() => _HouseListState();
}

class _HouseListState extends State<HouseList> {
  List houseData = [];
  // 是否加载中
  bool isLoading = false;

  //获取房屋列表
  @override
  void initState() {
    super.initState();
    getMyRoomsList();

    //重新获取房屋列表数据
    event.on<DeleteHouseEvent>().listen((e) {
      getMyRoomsList();
    });
  }

  getMyRoomsList() async {
    try {
      setState(() {
        isLoading = true;
      });
      final res = await http.get('/room');
      print(res);
      if (res['code'] != 10000) return ToastUtil.showError('获取失败');

      //延迟2秒
      Future.delayed(Duration(seconds: 2), () {
        setState(() {
          isLoading = false;
          houseData = res['data'];
          ToastUtil.showSuccess('获取房屋列表成功');
        });
      });
    } catch (e) {
      ToastUtil.showError('网络请求错误');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 设置导航条
      appBar: AppBar(
        title: Text('我的房屋'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
        //左侧返回按钮
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      // 设置主体内容
      body: isLoading
          ? Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  CircularProgressIndicator(),
                  SizedBox(height: 10),
                  Text('加载中...'),
                ],
              ),
            )
          : Container(
              padding: EdgeInsets.all(10),
              child: Column(
                children: [
                  //列表区域
                  Expanded(
                    child: ListView.separated(
                      shrinkWrap: true,
                      itemBuilder: (ctx, index) {
                        return HouseItem(houseItem: houseData[index]);
                      },
                      separatorBuilder: (ctx, index) {
                        return Container(height: 10);
                      },
                      itemCount: houseData.length,
                    ),
                  ),

                  //按钮区域
                  Container(
                    padding: EdgeInsets.only(top: 10),
                    width: MediaQuery.of(context).size.width,
                    height: 60,
                    child: ElevatedButton(
                      onPressed: () {
                        Navigator.pushNamed(context, '/add_house');
                      },
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [Icon(Icons.add), Text('添加房屋')],
                      ),
                    ),
                  ),
                ],
              ),
            ),
    );
  }
}
